package Admin;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

import javax.swing.JOptionPane;

import GroupClass.ConnectRMI;
import Project_etow.EtowInterface;

public class EditUnit extends javax.swing.JDialog {
    ConnectRMI connectRMI = new ConnectRMI();
	private int id;
	private int numberOfFloor;
	private int result;
	private String checkedit;
	
	public String getCheckedit() {
		return checkedit;
	}

	public void setCheckedit(String checkedit) {
		this.checkedit = checkedit;
	}
	
	private String idflor;

    public String getIdtow() {
		return idflor;
	}

	public void setIdtow(String idflor) {
		this.idflor = idflor;
	}
    /**
     * Creates new form Edit_Town
     */
    public EditUnit(java.awt.Frame parent, boolean modal, String idflor1) {
        super(parent, modal);
        idflor = idflor1;
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        lblFloorID = new javax.swing.JLabel();
        lblUnitName = new javax.swing.JLabel();
        lblUnitPrice = new javax.swing.JLabel();
        txtFFloorID = new javax.swing.JTextField();
        txtFUnitName = new javax.swing.JTextField();
        txtFUnitPrice = new javax.swing.JTextField();
        btnSubmit = new javax.swing.JButton();
        btnReset = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        setTitle("Edit Unit");
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        lblFloorID.setText("FloorID");
        txtFFloorID.setText(idflor);
        txtFFloorID.setEnabled(false);
        
        txtFUnitName.setText(UnitFree.getA1());
        txtFUnitPrice.setText(UnitFree.getA2());
        
        lblUnitName.setText("UnitName");

        lblUnitPrice.setText("UnitPrice");

        btnSubmit.setText("Submit");
        btnSubmit.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				try {
				    Registry registry = LocateRegistry.getRegistry(connectRMI.host,connectRMI.port);
                                    EtowInterface obj=(EtowInterface)registry.lookup(connectRMI.regis);
					String unitName = txtFUnitName.getText();
					String unitPrice = txtFUnitPrice.getText();
					String	decorate = "0";

					id = Integer.parseInt(UnitFree.getA3());
					String result = obj.EditUnit(unitName,unitPrice,decorate, id );
					if (result.equals("ThanhCong")) {
						JOptionPane.showMessageDialog(null, "Update Successful");
						setCheckedit("close");
						setVisible(false);						
					}
					else {
						JOptionPane.showMessageDialog(null, "Update Fail");
					}
				}catch(Exception ex){
					
				}
			}
		});

        btnReset.setText("Reset");
        btnReset.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {			
				txtFUnitName.setText("");
				txtFUnitPrice.setText("");
			}
		});

        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				setCheckedit("close");
				setVisible(false);					
			}
		});
        

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(61, 61, 61)
                        .addComponent(btnSubmit)
                        .addGap(30, 30, 30)
                        .addComponent(btnReset)
                        .addGap(27, 27, 27)
                        .addComponent(btnCancel))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(52, 52, 52)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(lblFloorID)
                            .addComponent(lblUnitName)
                            .addComponent(lblUnitPrice)
                            )
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtFFloorID, javax.swing.GroupLayout.PREFERRED_SIZE, 207, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtFUnitName, javax.swing.GroupLayout.PREFERRED_SIZE, 207, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtFUnitPrice, javax.swing.GroupLayout.PREFERRED_SIZE, 207, javax.swing.GroupLayout.PREFERRED_SIZE)
                           )))
                .addContainerGap(60, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblFloorID)
                    .addComponent(txtFFloorID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblUnitName)
                    .addComponent(txtFUnitName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblUnitPrice)
                    .addComponent(txtFUnitPrice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    
                    )
                .addGap(48, 48, 48)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSubmit)
                    .addComponent(btnReset)
                    .addComponent(btnCancel))
                .addContainerGap(22, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(EditUnit.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(EditUnit.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(EditUnit.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(EditUnit.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the dialog
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
            	EditUnit dialog = new EditUnit(new javax.swing.JFrame(), true, null);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify

    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnReset;
    private javax.swing.JButton btnSubmit;

    private javax.swing.JLabel lblFloorID;
    private javax.swing.JLabel lblUnitName;
    private javax.swing.JLabel lblUnitPrice;
    private javax.swing.JTextField txtFFloorID;
    private javax.swing.JTextField txtFUnitName;
    private javax.swing.JTextField txtFUnitPrice;
    // End of variables declaration
}
